package com.caigou.service.impl;

import com.caigou.bean.cms.entity.ArticleImage;
import com.caigou.mapper.cms.ArticleImageMapper;
import com.caigou.pojo.ArticleImageData;
import com.caigou.service.ArticleImageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("articleImageService")
public class ArticleImageServiceImpl implements ArticleImageService {

    @Resource
    private ArticleImageMapper articleImageMapper;

    @Override
    public ArticleImage saveArticleImageData(String articleId, Integer imageId, String imageUrl, String imageInfo, String imageAlt, Integer showIndex) {
        ArticleImage articleImage = new ArticleImage();
        articleImage.setArticleId(articleId);
        articleImage.setImageId(imageId);
        articleImage.setImageUrl(imageUrl);
        articleImage.setImageInfo(imageInfo);
        articleImage.setImageAlt(imageAlt);
        articleImage.setShowIndex(showIndex);
        articleImageMapper.saveArticleImageData(articleImage);
        return articleImage;
    }

    @Override
    public List<ArticleImageData> loadArticleImageData(String articleId) {
        return articleImageMapper.loadArticleImageData(articleId);
    }

    @Override
    public int updateArticleImageData(Integer id, Integer imageId, String imageUrl, String imageInfo, String imageAlt, Integer showIndex, String articleId) {
        if (articleImageMapper.loadArticleImageData(articleId).size() > 0){
            ArticleImage articleImage = new ArticleImage();
            articleImage.setId(id);
            articleImage.setImageId(imageId);
            articleImage.setImageUrl(imageUrl);
            articleImage.setImageInfo(imageInfo);
            articleImage.setImageAlt(imageAlt);
            articleImage.setShowIndex(showIndex);
            return articleImageMapper.updateArticleImageData(articleImage);
        } else {
            ArticleImage articleImage = new ArticleImage();
            articleImage.setArticleId(articleId);
            articleImage.setImageId(imageId);
            articleImage.setImageUrl(imageUrl);
            articleImage.setImageInfo(imageInfo);
            articleImage.setImageAlt(imageAlt);
            articleImage.setShowIndex(showIndex);
            articleImageMapper.saveArticleImageData(articleImage);
            return 1;
        }
    }

    @Override
    public int deleteArticleImageData(Integer id) {
        return articleImageMapper.deleteArticleImageData(id);
    }

    @Override
    public int deleteArticleImageByArticleId(String articleId){
        return articleImageMapper.deleteArticleImageByArticleId(articleId);
    }

}
